package com.training.admin.entity.principal;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * 系统用户
 * 
 * @author dan
 *
 */
@Entity
@Table(name = "principal")
public class Principal {
    
    /**
     * ID
     */
    @Id
    @GeneratedValue
    private Long id;
    /**
     * 
     */
    private String username;
    
    /**
     * 权限
     */
    private String authority;
    
    /**
     * 邮箱/登录
     */
    private String email;
    /**
     * 姓名
     */
    private String name;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date updateDate;
    
	/** 部门 ID */
    @Column(nullable=true)
    private Long departmentId;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    /**
     * 密码
     */
    private String password;
    
    private String originPassword;
    
	/**
	 * @return the departmentId
	 */
	public Long getDepartmentId() {
		return departmentId;
	}

	/**
	 * @param departmentId the departmentId to set
	 */
	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}

    
    /**
     * The default constructor
     */
    public Principal() {
    }
    
    /**
     * @param username
     */
    public Principal(String username) {
        this.id = new Long(1);
        this.username = username;
        this.email = "super_user@xx.com";
        this.password = encodePassword(generateDefaultPassword());
        this.authority = "ROLE_SUPER_USER";
        this.name = "超级管理员";
        this.disabled = false;
    }
    
    /**
     * 
     * @param date
     * @return
     */
	public Principal init(Date date){
		this.createDate = date;
		this.updateDate = date;
		return this;
	}
	
	
	public Principal update(Date date){
		this.updateDate=date;
		return this;
	}
    
    /**
     * @return
     */
    private String generateDefaultPassword() {
        return "123456";
    }
    
    /**
     * @param password
     * @return
     */
    public String encodePassword(String password) {
        String saltPassword = StringUtils.join(password, "{", this.email, "}");
        return DigestUtils.md5Hex(saltPassword);
    }
    
    /**
     * @return
     */
    public Principal resetPassword() {
        this.password = encodePassword(generateDefaultPassword());
        return this;
    }

    /**
     * @param password
     * @return
     */
    public Principal changePassword(String password) {
        this.password = encodePassword(password);
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @return the updateDate
     */
    public Date getUpdateDate() {
        return updateDate;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the originPassword
     */
    public String getOriginPassword() {
        return originPassword;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * @param updateDate the updateDate to set
     */
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param originPassword the originPassword to set
     */
    public void setOriginPassword(String originPassword) {
        this.originPassword = originPassword;
    }
}